import type { User } from '@/types/user'
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { getToken, setToken, removeToken } from '@/utils/auth'
export const useUserStore = defineStore(
  'cp-user',
  () => {
    //切换属性
    const flag = ref<Number>(1)
    const setFlag = (val: Number) => {
      flag.value = val
    }
    // 用户信息
    const user = ref<User>({
      token: getToken() || ''
    })
    const gettoken = () => {
      user.value.token = getToken() || ''
    }
    // 设置用户，登录后使用
    const setUser = (token: string) => {
      user.value.token = token
      setToken(token)
    }
    // 清空用户，退出后使用
    const delUser = () => {
      user.value.token = undefined
      removeToken()
    }
    return { user, setUser, delUser, flag, setFlag, gettoken }
  },
  {
    persist: true
  }
)
